<template>
	<view class="content">
		<button class="size" @tap="openDatabase">打开数据库</button>
		<button class="size" @tap="isOpenDatabase">判断数据库是否打开</button>
		<button class="size" @tap="closeDatabase">关闭数据库</button>
		<button class="size" @tap="transaction">执行事务</button>
		<button class="size" @tap="executeSql">执行增删改等操作的SQL语句</button>
		<button class="size" @tap="selectSql">执行查询的SQL语句SQL语句</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imgUrl: "",
				channels: []
			}
		},
		onLoad() {

		},
		methods: {
			openDatabase() {
				console.log(66)
				plus.sqlite.openDatabase({
					name: 'first',
					path: '_doc/test.db',
					success: function(e) {
						plus.nativeUI.alert('openDatabase success!');
					},
					fail: function(e) {
						plus.nativeUI.alert('openDatabase failed: ' + JSON.stringify(e));
					}
				});

			},
			isOpenDatabase() {
				const flag = plus.sqlite.isOpenDatabase({
					name: 'first',
					path: "_doc/test.db"
				})
				plus.nativeUI.alert(flag);
			},
			closeDatabase() {
				plus.sqlite.closeDatabase({
					name: 'first',
					success: function(e) {
						plus.nativeUI.alert('closeDatabase success!');
					},
					fail: function(e) {
						plus.nativeUI.alert('closeDatabase failed: ' + JSON.stringify(e));
					}
				});
			},
			transaction() {
				plus.sqlite.transaction({
					name: 'first',
					operation: 'begin',
					success: function(e) {
						plus.nativeUI.alert('transaction success!');
					},
					fail: function(e) {
						plus.nativeUI.alert('transaction failed: ' + JSON.stringify(e));
					}
				});

			},
			executeSql() {
				plus.sqlite.executeSql({
					name: 'first',
					sql: 'create table if not exists database("where" CHAR(110),"location" CHAR(100),"age" INT(11))',
					success: function(e) {
						console.log('executeSql success!');
						plus.sqlite.executeSql({
							name: 'first',
							sql: "insert into database values('北京','安乐林','11')",
							success: function(e) {
								plus.nativeUI.alert('executeSql success!');
							},
							fail: function(e) {
								plus.nativeUI.alert('executeSql failed: ' + JSON.stringify(e));
							}
						});
					},
					fail: function(e) {
						console.log('executeSql failed: ' + JSON.stringify(e));
					}
				});

			},
			selectSql() {
				plus.sqlite.selectSql({
					name: 'first',
					sql: 'select * from database',
					success: function(data) {
						console.log('selectSql success: ');
						for (var i in data) {
							plus.nativeUI.alert(JSON.stringify(data[i]));
						}
					},
					fail: function(e) {
						plus.nativeUI.alert('selectSql failed: ' + JSON.stringify(e));
					}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}

	.size {
		margin: 30rpx 0;
	}
</style>
